<?php

require_once __DIR__.DS.'blgData.php';

class BlgDB extends BlgData{
	
	protected function initializeAttributes(){
		parent::initializeAttributes();
		
		$this->attributes['link_identifier']= FALSE;
		$this->attributes['host']= blg_get_config('dbhost');		
		$this->attributes['database_name']= blg_get_config('dbname');		
		$this->attributes['database_username']= blg_get_config('dbuser');
		$this->attributes['database_password']= blg_get_config('dbpass');				
	}
	
	function __construct() {
		$this->initializeAttributes();
	}
	
	/***********************************************************
	 * 						SETTERS
	 ***********************************************************/
		
	public function select_database($database, $host= NULL){
		$this->attributes['database_name']= $database;
		if($host!==NULL)
			$this->attributes['host']= $host;
	}
	
	public function use_credentials($username, $password){
		$this->attributes['database_username']= $username;
		$this->attributes['database_password']= $password;		
	}
	
	/***********************************************************
	 * 					  CONNECTION
	 ***********************************************************/		
	
	public function connect(){				
		if(FALSE!==($this->attributes['link_identifier']= @mysql_connect($this->attributes['host'], $this->attributes['database_username'], $this->attributes['database_password']))){
			if(FALSE!==@mysql_select_db($this->attributes['database_name'], $this->attributes['link_identifier'])){				
				return TRUE;
			}else{
				blg_log('MYSQL: '.mysql_error(), TRUE);	
			}
		}else{
			blg_log('MYSQL: '.mysql_error(), TRUE);
		}
		
		return FALSE;
	}
	
	public function disconect(){
		if(@mysql_close($this->attributes['link_identifier'])){
			$this->attributes['link_identifier']= FALSE;
			return TRUE;
		}

		blg_log('MYSQL: '.mysql_error());
		return FALSE;
	}
	
	public function getLink(){
		return $this->attributes['link_identifier'];
	}
	
	/***********************************************************
	 * 						  QUERY
	 ***********************************************************/		
	
	public function query($query){
		return mysql_query($query, $this->attributes['link_identifier']);
	}
	
	
	/***********************************************************
	 * 						NOT ABSTRACT
	 ***********************************************************/	
	public function getURL(){}
	public function save(){}
	public function delete(){}	

}